/*
* Copyright (C) 2010-2011 dppc.org
* All rights reserved.
*/
package com.seemsea.web.app;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.gson.Gson;
import com.seemsea.entity.admin.idiom.Idiom;
import com.seemsea.service.admin.IdiomService;
import com.seemsea.utils.ControllerUtils;

/**
 *成语应用
 *
 *@author chirowong
 *@version 1.0 2011-6-15
 *@since 1.0
 */
@Controller
public class AppIdiomController {
	@Autowired
	private IdiomService idiomService;
	
	@RequestMapping(value="/app/idiom.nhtml")
    public String init(Model model,HttpServletRequest request) {
		String type = request.getParameter("type"); 
		if(type == null || "".equals(type)){
			type = "seemsea";
		}
		return "/app/idiom/"+type+"/index";
	}
	
	@RequestMapping(value="/app/getIdiomItem.nhtml")
	public String getIdiomItem(Model model,HttpServletRequest request,HttpServletResponse response){
		String type = request.getParameter("type"); 
		if(type == null || "".equals(type)){
			type = "seemsea";
		}
		String code = request.getParameter("p");
		Idiom idiom = idiomService.get(code);
		model.addAttribute("idiom", idiom);
		return "/app/idiom/"+type+"/detail";
	}
	
	@RequestMapping(value="/app/queryIdiom.nhtml",method = RequestMethod.POST)
	public String queryIdiom(IdiomForm idiomForm,Model model,HttpServletRequest request){
		String type = request.getParameter("type"); 
		if(type == null || "".equals(type)){
			type = "seemsea";
		}
		model.addAttribute("idiomForm",idiomForm);
		return "/app/idiom/"+type+"/queryList";
	}
	
	@RequestMapping(value="/appin/queryIdiom.nhtml",method = RequestMethod.POST)
	public void queryIdiomByAjax(IdiomForm idiomForm,Model model,HttpServletResponse response){
		String queryCondition = idiomForm.getQueryCondition();
		String queryInfo = idiomForm.getQueryInfo();
		Idiom idiom = new Idiom();
		if(queryCondition != null && queryCondition.equals("name")){
			idiom.setName(queryInfo);
		}
		if(queryCondition != null && queryCondition.equals("spinyin")){
			idiom.setSpinyin(queryInfo);
		}
		String strPage = idiomForm.getPage();
		int page = 0;
		if(strPage !=null && !"".equals(strPage)) page = Integer.parseInt(strPage);
		page += 1;
		idiom.setPage(page);
		List<Idiom> idioms = idiomService.getIdioms(idiom);
        Gson gson = new Gson();
        String result = gson.toJson(idioms);
        ControllerUtils.responseClientWithJson(response, result);
	}
	
	@RequestMapping(value="/appin/queryIdiomCount.nhtml",method = RequestMethod.POST)
	public void queryIdiomCountByAjax(IdiomForm idiomForm,Model model,HttpServletResponse response){
		String queryCondition = idiomForm.getQueryCondition();
		String queryInfo = idiomForm.getQueryInfo();
		Idiom idiom = new Idiom();
		if(queryCondition != null && queryCondition.equals("name")){
			idiom.setName(queryInfo);
		}
		if(queryCondition != null && queryCondition.equals("spinyin")){
			idiom.setSpinyin(queryInfo);
		}
		int idiomcount = idiomService.getIdiomsCount(idiom);
        Gson gson = new Gson();
        String result = gson.toJson(idiomcount);
        ControllerUtils.responseClientWithJson(response, result);
	}
	
/*	@RequestMapping(value="/appin/queryIdiom.nhtml",method = RequestMethod.GET)
	public String queryIdiomByGet(IdiomForm idiomForm,Model model,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		String type = request.getParameter("type"); 
		if(type == null || "".equals(type)){
			type = "seemsea";
		}
		String queryCondition = idiomForm.getQueryCondition();
		String queryInfo = request.getParameter("queryInfo");
		Idiom idiom = new Idiom();
		if(queryCondition != null && queryCondition.equals("name")){
			idiom.setName(queryInfo);
		}
		if(queryCondition != null && queryCondition.equals("spinyin")){
			idiom.setSpinyin(queryInfo);
		}
		List<Idiom> idioms = idiomService.getIdioms(idiom);
		if(idioms.size() == 1){
			model.addAttribute("idiom", idioms.get(0));
			return "/app/idiom/"+type+"/detail";
		}
		if(idioms.size() == 0){
			model.addAttribute("flag",false);
		}
		model.addAttribute("idioms",idioms);
		return "/app/idiom/"+type+"/queryList";
	}*/
}
